package cn.edu.scau.cmi.dengshixin.client;

import java.util.ArrayList;
import java.util.Scanner;

import cn.edu.scau.cmi.dengshixin.composite.consistent.*;

public class ConsistentCompositeClient {
	
	private static Scanner in;

	public static void main(String[] args) {
		ArrayList<OrganizationConsistentComponent> departments = new ArrayList<OrganizationConsistentComponent>();
		ArrayList<OrganizationConsistentComponent> persons = new ArrayList<OrganizationConsistentComponent>();
		ArrayList<OrganizationConsistentComponent> newPersons = new ArrayList<OrganizationConsistentComponent>();
		in = new Scanner(System.in);
		while(true) {
			System.out.println("\n请录入新员工姓名(回车结束)：");
			while(true) {
				String name = in.nextLine();
				if(name.equals("")) break;
				ConsistentPerson person = new ConsistentPerson(name);
				newPersons.add(person);
			}
			if(newPersons.isEmpty()) continue;
			
			OrganizationConsistentComponent component = null;
			System.out.println("对以上新员工，请选择以下功能：1. 录入已有部门    2.创建新部门");
			while(true) {
				int choice = in.nextInt();
				in.nextLine();
				if(choice == 1) {
					if(departments.isEmpty()) {
						System.out.println("当前没有任何部门！");
						continue;
					}
					System.out.println("当前有以下部门或个人：");
					displayComponent(departments, persons);
					System.out.println("请选择：");
					while(true) {
						choice = in.nextInt();
						in.nextLine();
						if(choice > 0 && choice <= (departments.size() + persons.size())) break;
					}
					if(choice <= departments.size())
						component = departments.get(choice-1);
					else
						component = persons.get(choice-1-departments.size());
				} // choice == 1
				else if(choice == 2) {
					System.out.println("请输入新部门名字：");
					String dept_name = in.nextLine();
					System.out.println("请选择新部门隶属于那个部门：");
					System.out.println("0.null");
					if(!departments.isEmpty()) displayComponent(departments, persons);
					while(true) {
						choice = in.nextInt();
						in.nextLine();
						if(choice >= 0 && choice <= (departments.size() + persons.size())) break;
					}
					if(choice == 0) {	// 选择没有隶属哪个部门
						component = new OrganizationConsistentComposite(dept_name, null);
						departments.add(component);
					}
					else if(choice > 0 && choice <= departments.size()){	// 选择了隶属于哪个部门
						component = new OrganizationConsistentComposite(dept_name, departments.get(choice-1).getName());
						departments.get(choice-1).addChild(component);
						departments.add(component);
					}
					else {	// 选择了员工
						component = new OrganizationConsistentComposite(dept_name, null);
						persons.get(choice-1-departments.size()).addChild(component);
						continue;
					}
				} // choice == 2
				else {
					continue;
				}
				
				boolean flag = false;
				for(OrganizationConsistentComponent p : newPersons) {
					if(!component.addChild(p)) {
						flag = true;
						break;
					}
					persons.add(p);
					p.setOrganization(component.getName());
				}
				newPersons.clear();
				if(flag) {
					System.out.println("录入失败！\n");
					break;
				}
				
				System.out.println("录入成功！\n");
				displayTree(departments);
				break;
			}
		}
	} // main
	
	static void displayComponent(ArrayList<OrganizationConsistentComponent> departments, ArrayList<OrganizationConsistentComponent> persons) {
		int i = 1;
		for(OrganizationConsistentComponent dept : departments)	{
			System.out.println( i + ".[" + dept.getOrganization() + "]" + dept.getName());
			++i;
		}
		for(OrganizationConsistentComponent p : persons)	{
			System.out.println( i + ".[" + p.getOrganization() + "]" + p.getName());
			++i;
		}
	}
	
	static void displayTree(ArrayList<OrganizationConsistentComponent> departments) {
		for(OrganizationConsistentComponent dept : departments)	{
			if(dept.getOrganization() == null) {
				dept.doSomething();
			}
		}
	}

}
